# -*- coding:utf-8 -*-
from django.db import models
from django.utils.translation import ugettext as _
from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db.models import Q
import datetime
from this_site.models import Staff
from django.db.models.signals import post_save

class BaseClass(models.Model):
    name = models.CharField(u'название', max_length=250)
    description = models.TextField(u'описание', help_text=u'Описание класса')
    
    class Meta:
        abstract = True
    
    def __unicode__(self):
        return u'%s' % self.name
    
    
class AutoType(BaseClass):
    class Meta:
        verbose_name = u'тип авто'
        verbose_name_plural = u'типы авто'
    
class Service(BaseClass):    
    class Meta:
        verbose_name = u'услугу'
        verbose_name_plural = u'услуги'

class Price(models.Model):
    sum = models.FloatField(u'цена')
    service = models.ForeignKey(Service, verbose_name = u'услуга')
    autotype = models.ForeignKey(AutoType, verbose_name = u'класс авто')
       
    class Meta:
        verbose_name = u'цена'
        verbose_name_plural = u'цены'
        
    def __unicode__(self):
        return u'%s - %s руб.' % (self.autotype, self.sum)
    
class Client(models.Model):
    fio = models.CharField(u'ФИО', max_length=250)
    auto_number = models.CharField(u'гос. номер', max_length=6)
    auto_region = models.IntegerField(u'регион')
    phone = models.CharField(u'телефон', max_length=25, blank=True, null=True)
    phone1 = models.CharField(u'телефон', max_length=25, blank=True, null=True)
    discount = models.BooleanField(u'скидка 10%')
       
    class Meta:
        verbose_name = u'клиент'
        verbose_name_plural = u'клиенты'
        
    def __unicode__(self):
        return u'%s %sRUS' % (self.auto_number, self.auto_region)
    
    def save(self):
        self.auto_number = self.auto_number.upper() 
        super(Client, self).save()

class Journal(models.Model):
    datatime = models.DateTimeField(u'дата и время', default=datetime.datetime.now(), editable=True)
    client = models.ForeignKey(Client, verbose_name = u'клиент', blank=True, null=True)
    autotype = models.ForeignKey(AutoType, verbose_name = u'класс авто')
    service = models.ManyToManyField(Service, verbose_name = u'услуги')
    staff = models.ManyToManyField(Staff, verbose_name = u'сотрудники выполняущие работы')
    sum = models.FloatField(u'сумма')
    
    free = models.BooleanField(u'бесплатная мойка')
    family = models.BooleanField(u'семья')
    clearing = models.BooleanField(u'безнал')
    card = models.BooleanField(u'оплата по карте')
       
    class Meta:
        verbose_name = u'запись'
        verbose_name_plural = u'журнал'
        ordering = ['-datatime']
        
    def __unicode__(self):
        return u'%s' % self.datatime.strftime("%d.%m.%Y %H:%M")
    
    def get_service_str(self):
        result = ''
        for obj in self.service.all():
            result += '%s<br/>' % obj.name
        return result
    get_service_str.short_description = u'Услуги'
    get_service_str.allow_tags = True
    
    def get_staff_str(self):
        result = ''
        for obj in self.staff.all():
            result += '%s<br/>' % obj.fio
        return result
    get_staff_str.short_description = u'Работники'
    get_staff_str.allow_tags = True
    
#    def save(self):
#        sum = 0
#        for service in self.service.all():
#            print u'%s' % service
#            sum += service.price_set.get(autotype=self.autotype).sum
#        self.sum = sum
#        super(Journal, self).save()